package com.mvvm.start.base.utils

import android.app.Application
import android.content.Context
import android.os.Build
import android.provider.Settings
import com.google.gson.Gson
import com.mvvm.start.base.BaseApplication
import com.mvvm.start.base.BaseApplication.Companion.context
import com.mvvm.start.base.bean.PublicConfigResult
import com.mvvm.start.base.bean.UserInfoResult
import java.lang.Exception

/**
 * @author
 * @功能:通用工具类
 * @Creat 1/5/21 3:30 PM
 * @Compony 465008238@qq.com
 */
object CommonUtils {
    /**
     * 获取保存的用户Bean
     */
    fun getUserBaen(): UserInfoResult? {
        var userBeanString =
            SPUtils.getInstance(BaseApplication.context).getSharedPreference("userBean", "") as String
        var userBean = if (!userBeanString!!.isNullOrEmpty()) Gson().fromJson(
            userBeanString,
            UserInfoResult::class.java
        ) as UserInfoResult else null
        return userBean
    }

    /**
     * 保存用户信息
     */
    fun savaUserBen(userBeanJson: UserInfoResult?) {
        SPUtils.getInstance(BaseApplication.context).put("userBean", userBeanJson?.toJson())
    }

    fun getUserID(): String {
        return getUserBaen()?.id ?: ""
    }


    fun getUserName(): String {
        return getUserBaen()?.name ?: ""
    }

    fun getToken(): String {
        return getUserBaen()?.token ?: ""
    }

    fun isLogin(): Boolean {
        val userBeanString =
            SPUtils.getInstance(BaseApplication.context).getSharedPreference("userBean", "") as String
        return userBeanString.isNotEmpty()
    }

    fun isVip(): Boolean {
        return if (getUserBaen() == null) {
            false
        } else {
            getUserBaen()?.level != 0
        }
    }

    fun getAppVersionName(): String {
        val packageManager = BaseApplication.context.packageManager
        val packageInfo = packageManager.getPackageInfo(BaseApplication.context.packageName, 0);
        return packageInfo.versionName
    }

    fun getAppVersionCode(): Int {
        val packageManager = BaseApplication.context.packageManager
        val packageInfo = packageManager.getPackageInfo(BaseApplication.context.packageName, 0);
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            packageInfo.longVersionCode.toInt()
        } else {
            packageInfo.versionCode
        }
    }


    fun getStatusBarHight(context: Context): Int {
        val resourceId: Int =
            context.resources.getIdentifier("status_bar_height", "dimen", "android")
        val statusBarHeight: Int = context.resources.getDimensionPixelSize(resourceId)
        return statusBarHeight
    }



    /**
     * 获取设备 Android ID 设备恢复出场设置会被重置
     * 因为android10以后获取IMEI有限制暂时先用AndroidID
     * 后续可以更改为OAID
     */
    fun getAndroidId(context: Context): String {
        var androidid: String? = null
        try {
            androidid =
                Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
        } catch (e: Exception) {
            androidid = ""
        }
        return androidid.toString()
    }


    /**
     * 隐私协议是否显示过
     */
    fun getIsPrivacyShow() = (SPUtils.getInstance(BaseApplication.context)
        .getSharedPreference(ConstantUtils.PRIVACY_IS_SHOW, 0) as Int) == 1


    fun getPublicConfig(): PublicConfigResult? {
        var publicConfigResultJson = SPUtils.getInstance(BaseApplication.context)
            .getSharedPreference(ConstantUtils.PUBLIC_CONFIG, "") as String
        var publicConfigResult = if (!publicConfigResultJson!!.isNullOrEmpty()) Gson().fromJson(
            publicConfigResultJson,
            PublicConfigResult::class.java
        ) as PublicConfigResult else null
        return publicConfigResult
    }
}